#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys, os

from loguru import logger

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QIcon

from qt_material import apply_stylesheet

sys.path.append(os.getcwd())
from views.widgets import Ui_mainwindow
from data_management import DataManagementForm
from sequence_control import SequenceControlForm
from image_analysis import ImageAnalysisForm
from print_setting import PrintSettingForm
from opo_control import OpoControlForm
from acquisition_setting import AcquisitionSettingForm
from register import RegisterForm
from models import data_record
from models import experiment_record
from utils import log_config


class PABoneMainWindow(QMainWindow, Ui_mainwindow.Ui_MainWindow):
    def __init__(self, parent = None):
        super(PABoneMainWindow, self).__init__(parent)
        self.setupUi(self)

        self.setWindowIcon(QIcon("./data/static/fudan.ico"))

        # 加载已有的数据
        self.paiDataRecord = data_record.PAIDataRecord()
        # experimentRecord reserved for new experiment
        self.experimentRecord = experiment_record.PAIExperimentRecord()

        # display the DataManagementForm default
        self.dataManagementForm = DataManagementForm()
        self.dataManagementForm.setMainWindow(self)
        self.dataManagementForm.setDataRecord(self.paiDataRecord)

        self.horizontalLayout.addWidget(self.dataManagementForm)
        self.currentForm = self.dataManagementForm

        self.sequenceControlForm = SequenceControlForm()
        self.sequenceControlForm.setMainWindow(self)

        self.imageAnalysisForm = ImageAnalysisForm()
        self.imageAnalysisForm.setMainWindow(self)

        self.printSettingForm = PrintSettingForm()
        self.printSettingForm.setMainWindow(self)

        self.registerForm = RegisterForm()
        self.registerForm.setMainWindow(self)
        self.registerForm.setExperimentRecord(self.experimentRecord)

        self.opoControlForm = OpoControlForm()
        self.opoControlForm.setMainWindow(self)

        self.acquisitionSettingForm = AcquisitionSettingForm()
        self.acquisitionSettingForm.setMainWindow(self) 

        logger.debug("create an instance of PABoneMainWindow successfully")      
    
    def setPAIDataRecord(self, dataRecord):
        self.paiDataRecord = dataRecord
    
    def getPAIDataRecord(self):
        return self.paiDataRecord

    def newExperiment(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.registerForm)
        self.currentForm = self.registerForm

        self.currentForm.show()
    
    def imageAnalysis(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.imageAnalysisForm)
        self.currentForm = self.imageAnalysisForm

        self.currentForm.show()

    def printSetting(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)
        ##  设置注册的采集数据信息

        self.horizontalLayout.addWidget(self.printSettingForm)
        self.currentForm = self.printSettingForm

        self.currentForm.show()
    
    def returnHome(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.dataManagementForm)
        self.currentForm = self.dataManagementForm

        self.currentForm.show()
    
    def registerNextStep(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)
        ## 将注册数据插入datarecord
        
        self.horizontalLayout.addWidget(self.sequenceControlForm)
        self.currentForm = self.sequenceControlForm

        self.currentForm.show()

    def sequenceControlNextStep(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.opoControlForm)
        self.currentForm = self.opoControlForm

        self.currentForm.show()

    def sequenceControlLastStep(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.registerForm)
        self.currentForm = self.registerForm

        self.currentForm.show()

    def opoControlNextStep(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.acquisitionSettingForm)
        self.currentForm = self.acquisitionSettingForm

        self.currentForm.show()

    def opoControlLastStep(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.sequenceControlForm)
        self.currentForm = self.sequenceControlForm

        self.currentForm.show()
    
    def acquisitionLastStep(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.opoControlForm)
        self.currentForm = self.opoControlForm

        self.currentForm.show()        
        pass

    def acquisitionStart(self):
        pass

    def acquisitionEnd(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.imageAnalysisForm)
        self.currentForm = self.imageAnalysisForm

        self.currentForm.show()
    
    def imageAnalysisNextStep(self):
        self.currentForm.setParent(None)
        self.horizontalLayout.removeWidget(self.currentForm)

        self.horizontalLayout.addWidget(self.printSettingForm)
        self.currentForm = self.printSettingForm

        self.currentForm.show()


    ## update the experimentRecord, this method is called in Register, AcqSetting
    def updateExperimentRecord(self, experimentRecord):
        self.experimentRecord = experimentRecord

if __name__ == "__main__":

    # initialization of logging 
    log_config.LogConfig.LogConfig()

    app = QApplication(sys.argv)
    PAWin = PABoneMainWindow()

    apply_stylesheet(app, theme = "dark_teal.xml")
    PAWin.show()
    sys.exit(app.exec_())